package org.jeecg.modules.system.mapper;

import com.baomidou.mybatisplus.core.mapper.BaseMapper;
import com.caocaog.market.merchant.entity.MkMerchantBasic;
import org.apache.ibatis.annotations.Mapper;
import org.apache.ibatis.annotations.Param;
import org.apache.ibatis.annotations.Select;
import org.jeecg.common.system.vo.SystemSite;
import org.jeecg.modules.system.entity.SysUserSite;
import org.jeecg.modules.system.entity.SysUserSiteArea;

import java.util.List;

/**
 * <p>
 * 用户分站 Mapper 接口
 * </p>
 *
 * @Author liguang
 * @since 2021-11-23
 */
@Mapper
public interface SysUserSiteMapper extends BaseMapper<SysUserSite>{

	/**
	 * 查询当前登录用户的所属分站
	 * */
	@Select("SELECT id,site_type as siteType,name,contact_person as contactPerson,mobile FROM mk_site_basic WHERE id=#{siteId}")
	SysUserSite findSiteForSysUser(@Param("siteId") String siteId);

	/**
	 * 查询当前登录用户所属分站对应的区域信息
	 * */
	@Select("SELECT ar.id,ar.site_id as siteId, ar.province_adcode AS provinceAdcode,ar.province_name AS provinceName,ar.province_center AS provinceCenter,ar.city_adcode AS cityAdcode,ar.city_name AS cityName,ar.city_center AS cityCenter,ar.district_adcode AS districtAcode,ar.district_name AS districtName,ar.district_center AS districtCenter,ar.town_adcode AS townAdcode,ar.town_name AS townName,ar.town_center  AS townCenter from  mk_site_area ar WHERE  ar.site_id=#{siteId} ")
	List<SysUserSiteArea> findSiteAreaBySiteId(@Param("siteId") String siteId);

	/**
	 * 根据用户名查询该用户的所属分站
	 * */
	@Select("SELECT site.id,site.site_type AS siteType,site.name,site.contact_person AS contactPerson,site.mobile FROM mk_site_basic site " +
			"LEFT JOIN sys_user us ON site.id = us.site_id " +
			" WHERE us.username=#{username}  LIMIT 1 ")
	SysUserSite findSiteForUserName(@Param("username") String username);

	/**
	 * 根据用户名查询该用户的所属分站的区域
	 */
	@Select("SELECT ar.id,ar.site_id AS siteId,ar.province_adcode AS provinceAdcode,ar.province_name AS provinceName,ar.province_center AS provinceCenter," +
			"ar.city_adcode AS cityAdcode,ar.city_name AS cityName,ar.city_center AS cityCenter,ar.district_adcode AS districtAcode,ar.district_name AS districtName," +
			"ar.district_center AS districtCenter,ar.town_adcode AS townAdcode,ar.town_name AS townName,ar.town_center  AS townCenter " +
			"FROM mk_site_area  ar LEFT JOIN sys_user  us ON ar.site_id = us.site_id WHERE us.username=#{username}")
	List<SysUserSiteArea> findSiteAreaForUserName(@Param("username") String username);


	/**
	 * 根据商家id查询的商家信息
	 * @param merchantId
	 * @return
	 */
	@Select("select site_id as siteId, id,name,idcard,front_idcard_url as frontIdcardUrl,back_idcard_url as backIdcardUrl,hand_held_idcard_url as handHeldIdcardUrl,mobile,type,grade,state,alipay,aliname,check_fail_reason as checkFailReason  from mk_merchant_basic where id=#{merchantId}")
	MkMerchantBasic findMerchantForMerchantId(@Param("merchantId") String merchantId);

	/**
	 * 查询所有的分站信息
	 * */
	@Select(" select  id,site_type,name,contact_person as contactPerson,mobile  from mk_site_basic WHERE del_flag = 0")
	List<SystemSite> findAllSite();
}
